﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace App2
{
    /// <summary>
    /// 十五子游戏
    /// </summary>
    public class Chess
    {
        public const int N = 4;       //边长
        private List<int> ChessManList;//棋子序号
        public Chess()
        {

        }

        //初始化游戏
        public void Init()
        {
            //http://www.doc88.com/p-7092015263762.html
            Random rnd = new Random();
            List<int> list = new List<int>();
            for (int i = 0; i < N * N; i++)
            {
                list.Add(i);
            }
            this.ChessManList = new List<int>();
            while (list.Count > 0)
            {
                int i = list[rnd.Next(list.Count)];
                list.Remove(i);
                ChessManList.Add(i);
            }

            //计算倒置数
            int count = 0;
            for (int i = 0; i < ChessManList.Count; i++)
            {
                if (ChessManList[i] != ChessManList.Count - 1)
                {
                    for (int j = i + 1; j < ChessManList.Count; j++)
                    {
                        if (ChessManList[i] > ChessManList[j])
                        {
                            count++;
                        }
                    }
                }
            }
            //空白所在行号
            int rowIndex = 0;
            for (int i = 0; i < ChessManList.Count; i++)
            {
                if (ChessManList[i] == N * N - 1)
                {
                    rowIndex = i / N;
                    break;
                }
            }
            bool list2IsOdd = (count % 2 == 0) ? false : true;
            bool rowIsOdd = (rowIndex % 2 == 0) ? true : false;         //0行为奇数，1行为偶数
            if (N % 2 == 0) //N为偶数：序列为偶置序列，空格必须在偶数行。序列为奇置序列，空格必须在奇数行。
            {
                if (!list2IsOdd && rowIsOdd) //偶置序列   空格行号为奇数
                {
                    int temp = ChessManList[ChessManList.Count - 1];       //交换后两个元素，改变奇偶性
                    ChessManList[ChessManList.Count - 1] = ChessManList[ChessManList.Count - 2];
                    ChessManList[ChessManList.Count - 2] = temp;
                }
                else if (list2IsOdd && !rowIsOdd)//奇置序列   空格行号为偶数
                {
                    int temp = ChessManList[0];    //交换前两个元素，改变奇偶性
                    ChessManList[0] = ChessManList[1];
                    ChessManList[1] = temp;
                }
            }
            else //N为奇数，序列必须为偶置序列
            {
                if (list2IsOdd)
                {
                    if (rowIsOdd)
                    {
                        int temp = ChessManList[N];       //交换后两个元素，改变奇偶性
                        ChessManList[N] = ChessManList[N + 1];
                        ChessManList[N + 1] = temp;
                    }
                    else
                    {
                        int temp = ChessManList[0];    //交换前两个元素，改变奇偶性
                        ChessManList[0] = ChessManList[1];
                        ChessManList[1] = temp;
                    }
                }
            }
        }
        //获得棋子序号
        public List<int> GetChessManList()
        {
            List<int> list = new List<int>();
            foreach (var item in this.ChessManList)
            {
                list.Add(item);
            }
            return list;
        }
        //获得序号 0-14对应正常棋子，15是隐藏棋子
        public int GetNumber(int index)
        {
            if (index < 0 || index >= N * N)
            {
                throw new Exception("参数 index 错误");
            }
            return this.ChessManList[index];
        }
        public static int GetTotalCount()
        {
            return N * N;
        }
    }
}
